﻿using Microsoft.EntityFrameworkCore;
using System.Reflection;
using Work.Infrastructure;
using Work.Infrastructure.Implements;
using Work.Infrastructure.Interfaces;
using NLog.Web;
using Yitter.IdGenerator;
using Work.API;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
//上下文
builder.Services.AddDbContext<ConDbContext>(o =>
{
    o.UseMySql(builder.Configuration.GetConnectionString("Con"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("Con")));
});
//仓储
builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
//中介者
builder.Services.AddMediatR(o => o.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
//自动映射
builder.Services.AddAutoMapper(o =>
{
    o.AddProfile<MappingProfiles>();
});
//日志
builder.Host.UseNLog();
//雪花
YitIdHelper.SetIdGenerator(new IdGeneratorOptions());


builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(o =>
{
    var fliename = AppContext.BaseDirectory + "Work.API.xml";
    o.IncludeXmlComments(fliename, true);
});

var app = builder.Build();

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseCors(o => o.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
app.UseAuthorization();

app.MapControllers();

app.Run();
